package com.system.controller;

import com.system.Tools.FileTool;
import com.system.function.object.Supplier;
import com.system.function.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService SupplierService;
    @Autowired
    private FileTool fileTool;


    //查询
    @RequestMapping("/s")
    @ResponseBody
    public Map<String,Object> s(Supplier Supplier) {
        List<Supplier> allSupplier = SupplierService.getAllSupplier(Supplier);
        Map<String,Object> returnMap  = new HashMap<>();
        returnMap.put("total",SupplierService.getCountSupplier(Supplier));
        returnMap.put("rows",allSupplier);
        return returnMap;
    }

    //查询
    @RequestMapping("/select")
    @ResponseBody
    public List<Supplier> select(Supplier Supplier) {
        List<Supplier> allSupplier = SupplierService.getAllSupplier(Supplier);
        return allSupplier;
    }

    //添加
    @RequestMapping("/add")
    @ResponseBody
    public String add(Supplier Supplier,MultipartFile file) {
        Boolean upload = fileTool.upload(file,Supplier);
        if(!upload){
            return "false-img";
        }
        return SupplierService.addSupplier(Supplier);
    }

    //文件下载相关代码
    @RequestMapping("/download")
    public String downloadFile(Supplier Supplier, HttpServletResponse response) {

        Supplier supplierSearch = SupplierService.getSupplier(Supplier);
        String fileName = supplierSearch.getImgPath();// 设置文件名，根据业务需要替换成要下载的文件名
        if (fileName != null) {
            //设置文件路径
            String realPath = "D://file//";
            File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

    //修改
    @RequestMapping("/up")
    @ResponseBody
    public String up(Supplier Supplier) {
        return SupplierService.updateSupplier(Supplier);
    }

    //用户删除
    @RequestMapping("/del")
    @ResponseBody
    public String del(Supplier Supplier) {
        return SupplierService.delSupplier(Supplier);
    }


}
